import pygame, sys, os

#map class, holds level information
class map_c:
    """ Need to pass in a path here so the map file can separate out the maplist into it's own dir """
    def __init__(self, mapTitle, mapImageFile, aspectRatio, gameObj, playerNum):
        self.entrances = []
        self.exits = []
        self.traps = []
        self.title = mapTitle
        self.numberOfPlayers = playerNum
        self.aspectRatio=(aspectRatio, aspectRatio)
        
        self.loadSurface(mapImageFile)
        
        self.screen = gameObj.screen
        self.game = gameObj
    
    """
    reset, resets the players to have the entrances/exits they need for this map
    Should probably have the level image loading code in here too
    """
    def reset(self):
        print "Initiating map: " + self.title
        for player in self.game.players:
            print "Player: "+ str(player.number)
            for entrance in self.entrances:
                if entrance.playerNumber == player.number:
                    print "Added entrance"
                    player.useEntrance(entrance)
            for exit in self.exits:
                if exit.playerNumber == player.number:
                    exit.player = player

    #should only be called on actual map init
    def loadSurface(self, fileName):
        self.mapSurface = pygame.image.load(os.path.join("data", "levels", fileName))
        
        if self.aspectRatio != (1,1):
            self.mapSurface = pygame.transform.scale(self.mapSurface, (self.mapSurface.get_width()*self.aspectRatio[0], self.mapSurface.get_height()*self.aspectRatio[1]))
        
        self.width = self.mapSurface.get_width()
        self.height = self.mapSurface.get_height()

    def draw(self):
        self.screen.blit(self.mapSurface,(0,0))

    def addEntrance(self, entrance):
        self.entrances += [entrance]

    def addExit(self, exit):
        self.exits += [exit]

    def drawEntrances(self, screenOffset):
        for entrance in self.entrances:
            entrance.draw(screenOffset[0], screenOffset[1])

    def drawExits(self, screenOffset):
        for exit in self.exits:
            exit.draw(screenOffset[0], screenOffset[1])
    
    def checkExits(self, x, y):
        for exit in self.exits:
            if exit.isInside(x, y):
                return exit
        return None
